import PyPDF2
import torch
from sentence_transformers import SentenceTransformer

# 加载预训练的句子嵌入模型
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')

def preprocess_pdf(pdf_path):
    # 读取PDF文件
    with open(pdf_path, 'rb') as file:
        pdf_reader = PyPDF2.PdfReader(file)
        text = ''
        for page in pdf_reader.pages:
            text += page.extract_text()

    # 生成句子嵌入
    embeddings = model.encode(text.split('. '))

    return text, embeddings

# 对文献知识库中的所有PDF文件进行预处理
knowledge_base = {}
for pdf_file in ['paper1.pdf', 'paper2.pdf', ...]:  # 替换为实际的PDF文件列表
    text, embeddings = preprocess_pdf(pdf_file)
    knowledge_base[pdf_file] = {'text': text, 'embeddings': embeddings}
